package com.baolai.controller;

import com.alibaba.fastjson.JSONObject;
import com.baolai.entity.GoodsUser;
import com.baolai.result.Result;
import com.baolai.result.ResultUtil;
import com.baolai.service.GoodsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Api(tags = "入库出库的操作员")
@ResponseBody
@RestController
@RequestMapping("/goods-user")
public class GoodsUserController {

    @Autowired
    GoodsUserService goodsUserService;

    @ApiOperation(value = "操作员登录")
    @PostMapping("/login")
    public Result userLogin(@RequestBody JSONObject jsonObject, HttpSession session, HttpServletRequest request, HttpServletResponse response){
        return goodsUserService.userLogin(jsonObject.getString("account"),jsonObject.getString("password"),session,request,response);
    }

    @ApiOperation(value = "管理员添加操作员")
    @PostMapping("/sign")
    public Result userSign(@RequestBody GoodsUser goodsUser){
        return goodsUserService.userSign(goodsUser);
    }

    @ApiOperation(value = "操作员等处")
    @PostMapping("/logout")
    public Result userLogout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        Object userInfo = session.getAttribute("user");
        if(userInfo == null){
            return ResultUtil.redirect("/login");
        }else{
            return ResultUtil.error("登出失败");
        }

    }

    @ApiOperation(value = "分页查询操作员")
    @PostMapping("/list")
    public Result listUser(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(goodsUserService.listUser(jsonObject.getInteger("current"),jsonObject.getInteger("size")));
    }

    @ApiOperation(value = "重置操作员密码")
    @PostMapping("update")
    public Result updateUser(@RequestBody GoodsUser goodsUser){
        if(goodsUserService.updateUser(goodsUser)){
            return ResultUtil.success("重置成功");
        }
        return ResultUtil.error("重置失败");
    }

    @ApiOperation(value = "删除操作员")
    @PostMapping("delete")
    public Result deleteUser(@RequestBody JSONObject jsonObject){
        if(goodsUserService.deleteUser(jsonObject.getString("account"))){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.error("删除失败");
    }
}
